package advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AspectAdvice {
	
	@Before("execution(* service.PriceService.price(..))")
	public void beforePrice(){
		System.out.println("AspectAdvice before invoked");
	}
	
	@AfterReturning("execution(* price(..))")
	public void afterPrice(){
		System.out.println("AspectAdvice after invoked");
	}
	
	@Before("DefinePointCut.beforeYouhuiPointCut()")
	public void beforeYouhui(){
		System.out.println("AspectAdvice with definePointCut before invoked");
	}
	
	@AfterReturning("DefinePointCut.afterYouhuiPointCut()")
	public void afterYouhui(){
		System.out.println("AspectAdvice with definePointCut after invoked");
	}
	
	
	
	/**
	 * if around advice must use ProceedingJoinPoint
	 * for getting the joinPoint infomation else use JoinPoint
	 * @param pjp
	 * @return
	 * @throws Throwable
	 */
	@Around("DefinePointCut.searchAddressPointCut()")
	public String aroundSearchAddress(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("---joinPointAccess aroundSearchAddress---");
		System.out.println("args[0]:"+pjp.getArgs()[0]);
		String address = (String)pjp.proceed();
		System.out.println(address);
		System.out.println("AspectAdvice with definePointCut aroundSearchAddress invoked");
		System.out.println("---joinPointAccess aroundSearchAddress---");
		return address;
	}
	
	@Before("DefinePointCut.searchAddressPointCut()")
	public void beforeSearchAddress(JoinPoint pjp) throws Throwable{
		System.out.println("---joinPointAccess beforeSearchAddress---");
		System.out.println("args[0]:"+pjp.getArgs()[0]);
		System.out.println("AspectAdvice with definePointCut beforeSearchAddress invoked");
		System.out.println("---joinPointAccess beforeSearchAddress---");
	}
	
}
